package com.bdrm.controller.admin;

import com.bdrm.dto.SetmealDTO;
import com.bdrm.dto.SetmealPageQueryDTO;
import com.bdrm.result.PageResult;
import com.bdrm.result.Result;
import com.bdrm.service.SetmealService;
import com.bdrm.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "套餐接口")
@Slf4j
@RequestMapping("admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @ApiOperation("套餐分页查询")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("添加套餐")
    @PostMapping("/save")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
//        log.info("套餐分页查询:{}",setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @PostMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("修改套餐信息")
    @PutMapping("/update")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        setmealService.deleteBach(ids);
        return Result.success();
    }


    @ApiOperation("修改套餐状态")
    @PostMapping("/{status}/{id}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status,@PathVariable Long id){
        setmealService.updateStatus(status, id);
        return Result.success();
    }
}
